﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;

namespace API.Controllers
{
    /// <summary>
    /// 自定义模块
    /// </summary>
    [RoutePrefix("Api/CustomDetails")]
    public class CustomDetailsController : ApiController
    {
        B_CustomDetails _bcd;
        public CustomDetailsController()
        {
            _bcd = new B_CustomDetails();
        }
        /// <summary>
        /// 根据模板的id拿到对应自定义分类的基本信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("GetCustomByTemplateID/{id}")]
        [HttpGet]
        public IHttpActionResult GetCustomByTemplateID(int id)
        {
            //var customdetails = new B_CustomDetails().GetAll();
            //var customtype = new B_CustomType().GetAll();
            //var data = from cd in customdetails
            //           join ct in customtype on cd.CTypeID equals ct.CTypeID
            //           where cd.TemplateID == id
            //           orderby ct.CPower ascending
            //           group cd by ct into g
            //           select new Param_CustomByTemplateID
            //           {
            //               CTypeID = g.Key.CTypeID,
            //               CName = g.Key.CName,
            //               CPower = g.Key.CPower,
            //               IsShow = g.Key.IsShow,
            //               ListCustomDetails = (from gg in g
            //                                    select new Param_CustomDetails
            //                                    {
            //                                        CDetailsID = gg.CDetailsID,
            //                                        CDTitle = gg.CDTitle,
            //                                        CTitleGrade = gg.CTitleGrade,
            //                                        CDDescribe = gg.CDDescribe,
            //                                        IsOnTrue = gg.IsOnTrue,
            //                                        CTypeID = gg.CTypeID,
            //                                        TemplateID = gg.TemplateID
            //                                    }).ToList()
            //           };
            return Ok(new Result<List<IDAL.Param_Custom>> {
                Data= _bcd.GetDataByTemplate(id)
            });
        }

        /// <summary>
        /// 获取所有自定义信息（有分页的）
        /// </summary>
        /// <returns></returns>
        [Route("GetAll")]
        [HttpGet]
        public IHttpActionResult GetAll(int page, int size)
        {
            B_CustomType bt = new B_CustomType();
            var data = from b in _bcd.GetAll()
                       join t in bt.GetAll()
                       on b.CTypeID equals t.CTypeID
                       select new Common.Param_CustomDetails
                       {
                           CDetailsID = b.CDetailsID,
                           CDTitle = b.CDTitle,
                           CTitleGrade = b.CTitleGrade,
                           CDDescribe = b.CDDescribe,
                           IsOnTrue = b.IsOnTrue,
                           CTypeID = b.CTypeID,
                           TemplateID = b.TemplateID,
                           CName = t.CName
                       };
            return Ok(new Result<List<Param_CustomDetails>>()
            {
                Data = data.Skip((page - 1) * size).Take(size).ToList(),
                Msg = data.Count().ToString()
            });
        }

        /// <summary>
        /// 获取所有自定义信息
        /// </summary>
        /// <returns></returns>
        [Route("GetAll")]
        [HttpGet]
        public IHttpActionResult GetAll()
        {
            B_CustomType bt = new B_CustomType();
            var data = from b in _bcd.GetAll()
                       join t in bt.GetAll()
                       on b.CTypeID equals t.CTypeID
                       select new Param_CustomDetails
                       {
                           CDetailsID = b.CDetailsID,
                           CDTitle = b.CDTitle,
                           CTitleGrade = b.CTitleGrade,
                           CDDescribe = b.CDDescribe,
                           IsOnTrue = b.IsOnTrue,
                           CTypeID = b.CTypeID,
                           TemplateID = b.TemplateID,
                           CName = t.CName
                       };
            return Ok(new Result<List<Param_CustomDetails>>()
            {
                Data = data.ToList()
            });
        }

        /// <summary>
        /// 创建自定义经历
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Add")]
        [HttpPost]
        public IHttpActionResult Add(CustomDetails param)
        {
            try
            {
                _bcd.Add(param);
                return Ok(new Result<object>() { Code = 200 });
            }
            catch (Exception exp)
            {
                return Ok(new Result<object>()
                {
                    Code = 500,
                    Msg = exp.Message
                });
            }
        }

        /// <summary>
        /// 删除自定义经历
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <returns></returns>
        [Route("Delete/{id}")]
        [HttpGet]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                _bcd.Delete(id);
                return Ok(new Result<object>());
            }
            catch (Exception exp)
            {
                return Ok(new Result<object>()
                {
                    Code = 500,
                    Msg = exp.Message
                });
            }
        }

        /// <summary>
        /// 查询自定义经历详情
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <returns></returns>
        [Route("Find/{id}")]
        [HttpPost]
        public IHttpActionResult Find(int id)
        {
            try
            {
                var data = _bcd.FindById(id);
                if (data == null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "数据为空"
                    });
                }
                return Ok(new Result<Param_CustomDetails>
                {
                    Data = new Param_CustomDetails
                    {
                        CDetailsID = data.CDetailsID,
                        CDTitle = data.CDTitle,
                        CTitleGrade = data.CTitleGrade,
                        CDDescribe = data.CDDescribe,
                        IsOnTrue = data.IsOnTrue,
                        CTypeID = data.CTypeID,
                        TemplateID = data.TemplateID
                    }
                });
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }

        /// <summary>
        /// 修改自定义经历
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Update/{id}")]
        [HttpGet]
        public IHttpActionResult Update(int id, CustomDetails param)
        {
            try
            {
                if (_bcd.FindById(id) == null)
                {
                    return Ok(new Result<object>() { Code = 404, Msg = "数据为空" });
                }
                _bcd.Update(id, param);
                return Ok(new Result<CustomDetails>() { Code = 200 });
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }

    }
}
